Vue.prototype.$ajax = axios;
axios.defaults.baseURL="http://localhost:2333/taofuxn";


var app = new Vue({
    el: '#app',
    data: {
        input5:'',
        tableData: [],
        loading: true,
        dialogVisible: false,
        labelPosition: 'right',
        formLabelAlign: {
            orderId:'',
            goodsId:'',
            consumerId:'',
            consumerName: '',
            createTime: '',
            consumerAddress: '',
            goodsName: '',
            consumerPhone:'',
            postage:'',
            sumPrice:'',
            fullCutPrice:'',
            note:''
        },
        multipleSelection: [],
        isEdit: false,
        editData: null,
        rules: {
            date: [
                { required: true, message: '请选择日期', trigger: 'blur' },
            ],
            name: [
                { required: true, message: '请输入姓名', trigger: 'blur' },
                { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
            ],
            address: [
                { required: true, message: '请输入地址', trigger: 'blur' },
                { min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur' }
            ]
        }
    },
    created() {
        this.initData()

    },
    methods: {

        initData() {
            axios.post("/sale/getOrders", {
                page: 0,
                pagesize: 10
            }).then((response) => {

                if(response.data.code == 0){
                    this.tableData = response.data.obj.infos
                }
                this.loading = false
            }).catch((error) => {
                console.log(error);
            });

        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done()
                })
                .catch(_ => { })
        },
        handleSelectionChange(val) {
            this.multipleSelection = val
        },
        handleEdit(index, row) {
            this.isEdit = true
            this.editData = index
            this.formLabelAlign = JSON.parse(JSON.stringify(row))
            this.dialogVisible = true
        },
        handleDelete(index, row) {
            this.del(index, row)
        },

        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let message = null
                    if (this.isEdit) {

                        this.submitByPost(this.formLabelAlign,(response)=>{
                            message = '编辑成功'
                            Object.assign(this.tableData[this.editData], this.formLabelAlign)
                        },(error)=>{
                            message = '编辑失败，请稍后重试！！'
                        })

                    } else {
                        message = '新增成功'
                        this.tableData.unshift(this.formLabelAlign)
                    }
                    this.dialogVisible = false
                    this.$message({
                        message: message,
                        type: 'success'
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            })
        },
        submitByPost: function (data, f, f2) {
            axios.post('/sale/modifyOrder',data).then(f).catch(f2);
        },
        add() {
            this.isEdit = null
            this.dialogVisible = true
            this.formLabelAlign = {
                name: '',
                date: '',
                address: ''
            }
        },
        del(type, index, row) {
            // console.log(sale)
            if (type == 'more') {
                if (!this.multipleSelection.length) {
                    this.$message({
                        type: 'error',
                        message: '请选择需要删除的行'
                    })
                }
                return false
            } else {

            }

            console.log(this.multipleSelection)

            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.tableData.splice(index, 1)
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            });
        }
    }
})

